﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Web;

namespace TEAA
{
    public class SystemProperties
    {

        /// <summary>
        /// Name of the system properties xml file
        /// </summary>
        private const string systemPropertiesFileName = "system_properties.xml";

        /// <summary>
        /// Path to system properties xml file
        /// </summary>
        private static string systemPropertiesPath;

        /// <summary>
        /// Loaded XML system properties
        /// </summary>
        private static XDocument systemPropertiesXml;


        private const string systemPropertiesElementText = "systemProperties";
        private const string systemRootLocationElementText = "systemRootLocation";
        private const string xmlSchemaRelativePathElementText = "xmlSchemaRelativePath";
        private const string xmlSchemaNamespaceElementText = "xmlSchemaNamespace";
        private const string bufferPageSizeText = "bufferPageSize";



        /// System Properties

        /// <summary>
        /// Database system root location path, system catalog and database files are stored here
        /// </summary>
        private static string systemRootLocation = HttpContext.Current.Server.MapPath("~") + "Databases";

        /// <summary>
        /// Path of XML schema file relative to application working directory
        /// </summary>
        private static string xmlSchemaRelativePath = "databaseCatalogSchema.xsd";

        /// <summary>
        /// Namespace of the XML schema to use for validation of database catalogs
        /// </summary>
        private static string xmlSchemaNamespace = "urn:databaseCatalogSchema";

        /// <summary>
        /// Buffer page size
        /// </summary>
        private static int bufferPageSize = 8192;

        /// <summary>
        /// Static constructor for system properties
        /// </summary>
        static SystemProperties()
        {
            systemPropertiesPath = HttpContext.Current.Server.MapPath("~") + Path.DirectorySeparatorChar + systemPropertiesFileName;
            if (File.Exists(systemPropertiesPath))
                systemPropertiesXml = XDocument.Load(systemPropertiesPath);
            else
            {
                systemPropertiesXml = new XDocument();
                XElement systemPropertiesElement = new XElement(systemPropertiesElementText);
                XElement systemRootlocationElement = new XElement(systemRootLocationElementText, systemRootLocation);
                XElement xmlSchemaRelativePathElement = new XElement(xmlSchemaRelativePathElementText, xmlSchemaRelativePath);
                XElement xmlSchemaNamespaceElement = new XElement(xmlSchemaNamespaceElementText, xmlSchemaNamespace);
                XElement bufferPageSizeElement = new XElement(bufferPageSizeText, bufferPageSize);

                systemPropertiesElement.Add(systemRootlocationElement);
                systemPropertiesElement.Add(xmlSchemaRelativePathElement);
                systemPropertiesElement.Add(xmlSchemaNamespaceElement);
                systemPropertiesElement.Add(bufferPageSizeElement);
                systemPropertiesXml.Add(systemPropertiesElement);

                systemPropertiesXml.Save(systemPropertiesPath);
            }
        }

        public static string SystemRootLocation
        {
            get
            {
                return systemRootLocation;
            }
            set
            {
                systemRootLocation = value;
                systemPropertiesXml.Descendants().
                    Where(e => e.Name == systemRootLocationElementText).
                    Single().Value = systemRootLocation;
                systemPropertiesXml.Save(systemPropertiesPath);
            }
        }

        public static string XmlSchemaRelativePath
        {
            get
            {
                return xmlSchemaRelativePath;
            }
            set
            {
                xmlSchemaRelativePath = value;
                systemPropertiesXml.Descendants().
                    Where(e => e.Name == xmlSchemaRelativePathElementText).
                    Single().Value = xmlSchemaRelativePath;
                systemPropertiesXml.Save(systemPropertiesPath);
            }
        }

        public static string XmlSchemaNamespace
        {
            get
            {
                return xmlSchemaNamespace;
            }
            set
            {
                xmlSchemaNamespace = value;
                systemPropertiesXml.Descendants().
                    Where(e => e.Name == xmlSchemaNamespaceElementText).
                    Single().Value = xmlSchemaNamespace;
                systemPropertiesXml.Save(systemPropertiesPath);
            }
        }

        public static int BufferPageSize
        {
            get
            {
                return bufferPageSize;
            }
            set
            {
                bufferPageSize = value;
                systemPropertiesXml.Descendants().
                    Where(e => e.Name == bufferPageSizeText).
                    Single().Value = bufferPageSize.ToString();
                systemPropertiesXml.Save(systemPropertiesPath);
            }
        }

    }
}
